package com.fresher.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;

import com.fresher.dao.AccountDao;
import com.fresher.dao.TransactionDao;
import com.fresher.model.Accountinfo;
import com.fresher.model.Transaction;

@Service
public class TransactionService {
	
	@Autowired
	MessageSource messageSource;
	
	@Autowired
	private AccountDao accountDao;

	@Autowired
	private TransactionDao transactionDao;

	// get list account from username
	public List<Accountinfo> getListAccount(String loginId) {
		List<Accountinfo> AccountList = null;
		AccountList = accountDao.getAccounts(loginId);
		return AccountList;

	}

	// get list transaction of current user
	public List<Transaction> getListTransaction(String loginId) {
		List<Transaction> TransactionList = transactionDao
				.getTransaction(loginId);

		return TransactionList;

	}

	// get list send transaction of current user
	public List<Transaction> getListSendTransaction(String loginId) {
		List<Transaction> TransactionList = transactionDao
				.getSendTransaction(loginId);

		return TransactionList;

	}

	// get list recieve transaction of current user
	public List<Transaction> getListReceiveTransaction(String loginId) {
		List<Transaction> TransactionList = transactionDao
				.getReceiveTransaction(loginId);

		return TransactionList;

	}

	// get list recent account to
	public List<Transaction> getListRecentTransaction(String loginId) {
		List<Transaction> TransactionList = transactionDao
				.getRecentTransaction(loginId);
		return TransactionList;

	}

	// search list transaction of current user
	public List<Transaction> searchListTransaction(String loginId,
			String startDate, String endDate, String transType) {
		Date Start = null, End = null;
		SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
		try {
			Start = formatter.parse(startDate);
			End = formatter.parse(endDate);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		List<Transaction> TransactionList = transactionDao.searchTransaction(
				loginId, Start, End, transType);

		return TransactionList;

	}

	// check int
	public static boolean isInteger(String input) {
		try {
			Integer.parseInt(input);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	// create transaction
	public String createTransactionService(String TargetAccount,
			String SourceAccount, String amount, String description,
			String dateString) {
		String message = "";
		int afterBalance, beforeBalance, amountTranfer;
		boolean sourceID, targetID;
		Date date = null;
		Locale locale = LocaleContextHolder.getLocale();
		if (description.length() > 200) {
//			message = "Invalid description!";
			message = messageSource.getMessage("label.invalidDescription", null, locale).toString();
			
		} else {
			// check if account from is number
			if (isInteger(SourceAccount) == false) {
//				message = "Invalid account from!";
				message = messageSource.getMessage("label.invalidAccountFrom", null, locale).toString();
			} else {
				// check if source account is existed
				sourceID = accountDao.checkAccount(Integer.parseInt(SourceAccount));
				if (sourceID == false) {
//					message = "Account from not available!";
					message = messageSource.getMessage("label.accountFromNA", null, locale).toString();
				} else{
					if (isInteger(TargetAccount) == false) {
//						message = "Invalid account to!";
						message = messageSource.getMessage("label.invalidAccountTo", null, locale).toString();
					} else{
						// check if target account is existed
						targetID = accountDao.checkAccount(Integer.parseInt(TargetAccount));
						// check amount is number
						if (isInteger(amount) == false) {
//							message = "Invalid amount!";
							message = messageSource.getMessage("label.invalidAmount", null, locale).toString();
						} else {
							amountTranfer = Integer.parseInt(amount);
							// check account to
							if (targetID == false) {
//								message = "Account to not available!";
								message = messageSource.getMessage("label.accountToNA", null, locale).toString();
							} else {
								beforeBalance = accountDao.getBalance(Integer.parseInt(SourceAccount));
								// check amount left on account
								if (beforeBalance < amountTranfer
										|| amountTranfer > 5000000) {
//									message = "Invalid amount!";
									message = messageSource.getMessage("label.invalidAmount", null, locale).toString();
								} else if (beforeBalance >= amountTranfer) {
									try {
										// parse date
										SimpleDateFormat formatter = new SimpleDateFormat();
										date = formatter.parse(dateString);
										// create transcation
										afterBalance = beforeBalance
												- amountTranfer;
										Transaction transcation = new Transaction(
												afterBalance, amountTranfer,
												beforeBalance, date,
												description);
										// add new transcation , account id from
										// and
										// account id to
										transactionDao.addTransaction(transcation,Integer.parseInt(SourceAccount),
														Integer.parseInt(TargetAccount));
//										message = "Tranfer Succsessful!";
										message = messageSource.getMessage("label.transSuccess", null, locale).toString();
										System.out.println(messageSource.getMessage("label.transSuccess", null, new Locale("vi")).toString());

									} catch (ParseException e) {
										// TODO Auto-generated catch block
										message = "Invalid datetime";
									}
								}//check amount
							}//check targetAccount
						}//check amount
					}//check targetAccount int
				}//check sourceaccount
			}//check sourceaccount int
		}//description length
		return message;
	}
}
